#-*- coding:gbk -*-
'''
Created on 2012-7-18

@author: xxy
'''
#AI Module
class State(object):
    def __init__(self, name):
        self.m_name = name

    def doActions(self):
        pass
    
    def checkConditions(self):
        pass

    def entryActions(self):
        pass

    def exitActions(self):
        pass

class FSM(object):
    def __init__(self):
        self.m_states = {}
        self.m_active_state = None
        
    def addState(self, state):
        self.m_states[state.m_name] = state
        
    def think(self):
        #print 'think'
        if self.m_active_state is None:
            return
        #print self.m_active_state
        self.m_active_state.doActions()
        new_state_name = self.m_active_state.checkConditions()
        if new_state_name is not None:
            self.setState(new_state_name)

    def setState(self, new_state_name):
        if self.m_active_state is not None:
            self.m_active_state.exitActions()
        self.m_active_state = self.m_states[new_state_name]
        self.m_active_state.entryActions()
